﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PMS.Client.Entities;
using PMS.Client.IBLL;
using PMS.Client.IDAL;
using PMS.Client.Utils;

namespace PMS.Client.BLL
{
    public class MenuService : IMenuService
    {
        IMenuAccess _menuAccess;
        public MenuService(IMenuAccess menuAccess)
        {
            _menuAccess = menuAccess;
        }
        public IEnumerable<MenuEntity> GetAllMenus(string key)
        {
            string json = _menuAccess.GetAllMenus(key);
            var result = JsonUtil.Deserializer<Result<MenuEntity[]>>(json);
            if (result == null)
                throw new Exception("菜单数据获取失败!");
            if (result.state != 200)
                throw new Exception(result.exceptionMessage);

            return result.data;
        }

        public int UpdateMenu(MenuEntity menu)
        {
            string menu_json = System.Text.Json.JsonSerializer.Serialize(menu);
            string json = _menuAccess.UpdateMenu(menu_json);
            var result = JsonUtil.Deserializer<Result<int>>(json);

            if (result == null)
                throw new Exception("菜单数据获取失败!");
            if (result.state != 200)
                throw new Exception(result.exceptionMessage);
            if (result.data == 0)
                throw new Exception("未更新任何数据");

            return result.data;
        }

        public int DeleteMenu(string id)
        {
            string json = _menuAccess.DeleteMenu(id);
            var result = JsonUtil.Deserializer<Result<int>>(json);

            if (result == null)
                throw new Exception("菜单数据获取失败!");
            if (result.state != 200)
                throw new Exception(result.exceptionMessage);
            if (result.data == 0)
                throw new Exception("未更新任何数据");

            return result.data;
        }
    }
}
